<?php

namespace App\Http\Controllers\Home;


use App\Http\Model\Articel;
use App\Http\Model\Category;
use App\Http\Model\Links;
use Illuminate\Http\Request;

use App\Http\Requests;
use SimpleSoftwareIO\QrCode\Facades\QrCode;


class IndexController extends CommonController
{
    public function index()
    {
        //点击量最高的6篇文章
        $hot=Articel::orderBy('art_view','desc')->take(6)->get();

         //图文列表（带分页）
        $data=Articel::orderBy('art_time','desc')->paginate(5);
        //dd($data);

        //友情链接
        $links=Links::orderBy('link_order','asc')->get();
        //dd($links);

        //网站配置
       return view('home.index',compact('hot','data','links'));
    }

    public function cate($cate_id)
    {
        $field=Category::find($cate_id);

        //图文列表（带分页）
        $data=Articel::where('cate_id',$cate_id)->orderBy('art_time','desc')->paginate(4);

        //查看次数自增
        Articel::where('cate_id',$cate_id)->increment('art_view');

        //读取当前分类子分类
        $song=Category::where('cate_pid',$cate_id)->get();

       return view('home.list',compact('field','data','song'));
    }

    public function news($art_id)
    {
        $field = Articel::Join('category','article.cate_id','=','category.cate_id')->where('art_id',$art_id)->first();

        //查看次数自增
        Articel::where('art_id',$art_id)->increment('art_view');

        $article['pre'] = Articel::where('art_id','<',$art_id)->orderBy('art_id','desc')->first();
        $article['next'] = Articel::where('art_id','>',$art_id)->orderBy('art_id','asc')->first();

        $data = Articel::where('cate_id',$field->cate_id)->orderBy('art_id','desc')->take(6)->get();

        return view('home.news',compact('field','article','data'));
    }

   public function qrcode(){
      QrCode::format('png')->size(260)->merge('/public/test.png',.15)->generate('http://baidu.com',public_path('qrcode.png'));

   }
}
